package pri.black.dogview.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.security.auth.Subject;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class TokenAuthentication implements Authentication {

    private Collection<? extends GrantedAuthority> authorities;

    private UserDetails userDetails = null;

    private boolean authed = false;

    private String username;
    private String password;

    private String role_prefix = "ROLE_";

    public TokenAuthentication(UserDetails userDetails) {
        this.userDetails = userDetails;
        authorities = userDetails.getAuthorities();
    }

    public TokenAuthentication(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new SimpleGrantedAuthority(role_prefix + "admin"));
    }

    @Override
    public Object getCredentials() {
        return this.password;
    }

    @Override
    public Object getDetails() {
        return userDetails;
    }

    @Override
    public Object getPrincipal() {
        return this.username;
    }

    @Override
    public boolean isAuthenticated() {
        return authed;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        this.authed = isAuthenticated;
    }

    @Override
    public String getName() {
        return "admin";
    }

    @Override
    public boolean implies(Subject subject) {
        return Authentication.super.implies(subject);
    }

    public UserDetails getUserDetails() {
        return userDetails;
    }
}
